package com.bill.robot.koalacam;

import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 门禁管理
 * @author litong
 * 2017年7月5日
 */
public class SysteM {

  /**
  * 获取门禁列表
  * @param cookieStore cookie值
  * @param url
  */
  public static void screen(CookieStore cookieStore, String url) throws Exception {
    System.out.println("Start GET /system/screen 获取门禁列表 ...");

    // 自定义HttpClient 设置CookieStore
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    HttpGet request = new HttpGet(url);

    CloseableHttpResponse response = httpclient.execute(request);
    String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

    // 解析JSON数据
    JSONObject resp = new JSONObject(responseBody);
    int result = resp.optInt("code", -1);
    if (result != 0) {
      System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
    } else {
      JSONArray array = resp.getJSONArray("data");
      for (int i = 0; i < array.length(); i++) {
        JSONObject screen = (JSONObject) array.get(i);
        System.out.println("id:" + screen.getInt("id") + ",camera_position:" + screen.getString("camera_position"));
      }
    }
  }

  /**
   * 获取所有可用本地主机
   * @param cookieStore
   * @param url
   * @throws Exception
   */
  public static void boxes(CookieStore cookieStore, String url) throws Exception {
    System.out.println("Start GET /system/boxes获取所有可用本地主机 ...");
    // 自定义HttpClient 设置CookieStore
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    HttpGet request = new HttpGet(url);

    CloseableHttpResponse response = httpclient.execute(request);
    String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

    // 解析JSON数据
    JSONObject resp = new JSONObject(responseBody);
    int result = resp.optInt("code", -1);
    if (result != 0) {
      System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
    } else {
      // 在线版 返回公司账号下绑定的所有主机
      // 离线版 如果是单机 只返回本机；如果是级联，则返回所有主机
      JSONArray array = resp.getJSONArray("data");
      for (int i = 0; i < array.length(); i++) {
        JSONObject box = (JSONObject) array.get(i);
        System.out.println("id:" + box.getInt("id") + " , box_address:" + box.getString("box_address") + " , box_token:"
            + box.getString("box_token"));
      }
    }
  }

  /**    
   * 创建一个门禁
   * @param cookieStore cookie值
   * @param url
   * @param Integer box_id 主机ID
   * @param camera_position 相机位置
   * @param camera_address 相机视频流地址
   * @throws Exception
   */
  public static void addScreen(CookieStore cookieStore, String url, Integer box_id, String camera_position,
      String camera_address) throws Exception {
    System.out.println("Start POST /system/screen 创建一个门禁 ...");

    // 自定义HttpClient 设置CookieStore
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    HttpPost request = new HttpPost(url);
    // 设置Content-Type
    request.setHeader("Content-Type", "application/json");

    JSONObject json = new JSONObject();
    json.put("box_id", box_id);
    json.put("camera_position", camera_position);
    json.put("camera_address", camera_address);
    request.setEntity(new StringEntity(json.toString(), "UTF-8"));

    CloseableHttpResponse response = httpclient.execute(request);
    String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

    // 解析JSON数据
    JSONObject resp = new JSONObject(responseBody);
    int result = resp.optInt("code", -1);
    if (result != 0) {
      System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
    } else {
      JSONObject screen = resp.getJSONObject("data");
      System.out.println("id:" + screen.getInt("id") + ",camera_position:" + screen.getString("camera_position"));
    }
  }

  /**    
   * 更新门禁信息
   * @param cookieStore cookie值
   * @param url
   * @param camera_position 相机位置
   * @param allow_visitor 是否允许访客进入
   * @throws Exception
   */
  public static void updateScreen(CookieStore cookieStore, String url, String camera_position, Boolean allow_visitor)
      throws Exception {
    System.out.println("Start PUT /system/screen/[id] 更新门禁信息 ...");

    // 自定义HttpClient 设置CookieStore
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    HttpPut request = new HttpPut(url);
    // 设置Content-Type
    request.setHeader("Content-Type", "application/json");

    JSONObject json = new JSONObject();
    json.put("camera_position", camera_position);
    json.put("allow_visitor", allow_visitor);

    request.setEntity(new StringEntity(json.toString(), "UTF-8"));

    CloseableHttpResponse response = httpclient.execute(request);
    String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

    // 解析JSON数据
    JSONObject resp = new JSONObject(responseBody);
    int result = resp.optInt("code", -1);
    if (result != 0) {
      System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
    } else {
      JSONObject screen = resp.getJSONObject("data");
      System.out.println("id:" + screen.getInt("id") + ",camera_position:" + screen.getString("camera_position"));
    }
  }

  /**    
   * 获取门禁信息
   * @param cookieStore cookie值
   * @param url /system/screen/[id]
   * @throws Exception
   */
  public static void getScreen(CookieStore cookieStore, String url) throws Exception {
    System.out.println("Start GET /system/screen/[id] 获取门禁信息 ...");

    // 自定义HttpClient 设置CookieStore
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    HttpGet request = new HttpGet(url);

    CloseableHttpResponse response = httpclient.execute(request);
    String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

    // 解析JSON数据
    JSONObject resp = new JSONObject(responseBody);
    int result = resp.optInt("code", -1);
    if (result != 0) {
      System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
    } else {
      JSONObject screen = resp.getJSONObject("data");
      System.out.println("id:" + screen.getInt("id") + ",camera_position:" + screen.getString("camera_position"));
    }
  }

  /**    
   * 删除门禁
   * @param cookieStore cookie值
   * @param url /system/screen/[id]
   * @throws Exception
   */
  public static void deleteScreen(CookieStore cookieStore, String url) throws Exception {
    System.out.println("Start DELETE /system/screen/[id] 删除门禁 ...");

    // 自定义HttpClient 设置CookieStore
    CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
    HttpDelete request = new HttpDelete(url);

    CloseableHttpResponse response = httpclient.execute(request);
    String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");

    // 解析JSON数据
    JSONObject resp = new JSONObject(responseBody);
    int result = resp.optInt("code", -1);
    if (result != 0) {
      System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
    } else {
      JSONArray array = resp.getJSONArray("data");
      for (int i = 0; i < array.length(); i++) {
        JSONObject screen = (JSONObject) array.get(i);
        System.out.println("id:" + screen.getInt("id") + ",camera_position:" + screen.getString("camera_position"));
      }
    }
  }
}